﻿Public Class SharedFoldersControl
    Private Class ImageKeys
        Public Const Movie As String = "m"
        Public Const TVSeries As String = "t"
        Public Const Share As String = "s"
    End Class

    Public Sub Init()
        ImageList1.Images.Add(ImageKeys.Movie, My.Resources.Movie)
        ImageList1.Images.Add(ImageKeys.TVSeries, My.Resources.TV)
        ImageList1.Images.Add(ImageKeys.Share, Framework.My.Resources.Share)

        lvwSharedFolders.BeginUpdate()
        For Each clsShare As Entities.Share In EntityHandlers.Shares.GetAll
            UpdateSharedFolder(clsShare, True)
        Next
        lvwSharedFolders.EndUpdate()
        SetupEvents(True)
    End Sub

    Private Sub SetupEvents(ByVal startListening As Boolean)
        If startListening Then
            AddHandler EntityHandlers.Shares.DeletedGUI, AddressOf EventHandler_ShareDeleted
            AddHandler EntityHandlers.Shares.UpdatedGUI, AddressOf EventHandler_ShareUpdated
        Else
            RemoveHandler EntityHandlers.Shares.DeletedGUI, AddressOf EventHandler_ShareDeleted
            RemoveHandler EntityHandlers.Shares.UpdatedGUI, AddressOf EventHandler_ShareUpdated
        End If
    End Sub

    Public Sub [Stop]()
        SetupEvents(False)
    End Sub


    Private Sub UpdateSharedFolder(ByVal share As Entities.Share, ByVal fromLoad As Boolean)
        If share Is Nothing Then Return
        Dim clsItem As ListViewItem = Nothing
        If Not fromLoad Then
            For Each clsTmpItem As ListViewItem In lvwSharedFolders.Items
                Dim clsShare As Entities.Share = CType(clsTmpItem.Tag, Entities.Share)
                If share.Id = clsShare.Id Then
                    clsItem = clsTmpItem
                    Exit For
                End If
            Next
        End If
        If clsItem Is Nothing Then
            clsItem = lvwSharedFolders.Items.Add(String.Empty)
            While clsItem.SubItems.Count < 5
                clsItem.SubItems.Add(String.Empty)
            End While
        End If
        clsItem.Text = share.Name
        clsItem.SubItems(1).Text = share.GroupMode.ToString
        clsItem.SubItems(2).Text = share.ItemTypes.ToString
        clsItem.SubItems(3).Text = CStr(IIf(share.Recursive, "Yes", "No"))
        clsItem.SubItems(4).Text = Framework.Serialization.ToCommaSepString(share.Paths)
        clsItem.Tag = share
        Select Case share.ItemTypes
            Case Entities.ItemType.Movies
                clsItem.ImageKey = ImageKeys.Movie
            Case Entities.ItemType.TVSeries
                clsItem.ImageKey = ImageKeys.TVSeries
            Case Else
                clsItem.ImageKey = ImageKeys.Share
        End Select
    End Sub

    Private Sub ViewSharedItemsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ViewSharedItemsToolStripMenuItem.Click
        ShowSharedItemsForSelectedShare()
    End Sub

    Private Sub lvwSharedFolders_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvwSharedFolders.MouseDoubleClick
        ShowSharedItemsForSelectedShare()
    End Sub

    Private Sub ShowSharedItemsForSelectedShare()
        If lvwSharedFolders.SelectedItems.Count <> 1 Then Beep() : Return
        Dim clsShare As Entities.Share = CType(lvwSharedFolders.SelectedItems(0).Tag, Entities.Share)
        FileShare.UI.FViewSharedItems.Show(Me, clsShare)
    End Sub

    Private Sub RescanForChangesMenuToolItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RescanForChangesMenuToolItem.Click
        If lvwSharedFolders.SelectedItems.Count <> 1 Then Beep() : Return
        Dim clsShare As Entities.Share = CType(lvwSharedFolders.SelectedItems(0).Tag, Entities.Share)
        Engine.Manager.RescanShare(clsShare)
    End Sub

    Private Sub RemoveShareToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveShareToolStripMenuItem.Click
        If lvwSharedFolders.SelectedItems.Count = 1 Then
            Dim clsShare As Entities.Share = CType(lvwSharedFolders.SelectedItems(0).Tag, Entities.Share)
            Select Case MessageBox.Show(Me, String.Format("Delete {0}?", clsShare.Name), "Delete Share", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
                Case Windows.Forms.DialogResult.Yes
                Case Else
                    Return
            End Select
            FileShare.UI.Manager.DeleteShare(clsShare)
        End If
    End Sub

    Private Sub EventHandler_ShareDeleted(ByVal share As Entities.Share)
        For Each clsItem As ListViewItem In lvwSharedFolders.Items
            If CType(clsItem.Tag, Entities.Share).Id = share.Id Then
                clsItem.Remove()
                Exit For
            End If
        Next
    End Sub

    Private Sub EventHandler_ShareUpdated(ByVal share As Entities.Share)
        UpdateSharedFolder(share, False)
    End Sub

    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
        If lvwSharedFolders.SelectedItems.Count = 1 Then
            Dim clsShare As Entities.Share = CType(lvwSharedFolders.SelectedItems(0).Tag, Entities.Share)
            Select Case MessageBox.Show(Me, String.Format("Use ""{0}"" as a template for your new share?", clsShare.Name), "New share", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
                Case DialogResult.Yes
                    FEditShare.CreateNew(Me, clsShare)
                Case Else
                    FEditShare.CreateNew(Me)
            End Select
        Else
            FEditShare.CreateNew(Me)
        End If
    End Sub

    Private Sub PropertiesToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PropertiesToolStripMenuItem.Click
        If lvwSharedFolders.SelectedItems.Count <> 1 Then
            Beep()
        Else
            Dim clsShare As Entities.Share = CType(lvwSharedFolders.SelectedItems(0).Tag, Entities.Share)
            FEditShare.Edit(Me, clsShare)
        End If
    End Sub

    Private Sub SharedFoldersContextMenu_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SharedFoldersContextMenu.Opening
        Dim blnJustOneSelected As Boolean = lvwSharedFolders.SelectedItems.Count = 1
        ViewSharedItemsToolStripMenuItem.Enabled = blnJustOneSelected
        RescanForChangesMenuToolItem.Enabled = blnJustOneSelected
        PropertiesToolStripMenuItem.Enabled = blnJustOneSelected
        RemoveShareToolStripMenuItem.Enabled = blnJustOneSelected
    End Sub
End Class
